﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Drawing.Imaging;
using DekstopVer.MemberService;

namespace DekstopVer
{
    /// <summary>
    /// Interaction logic for register.xaml
    /// </summary>
    public partial class register : Window
    {
        Graphics g;
        Bitmap bitmap;
        public register()
        {
            InitializeComponent();
        }

        private void CreateImage()
        {
   
            MemberServiceClient c = new MemberServiceClient();
            Random rand = new Random();


            string code = c.randomText();

            bitmap = new Bitmap(200, 150, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            g = Graphics.FromImage(bitmap);
            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Yellow);
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, 200, 150);

            SolidBrush b = new SolidBrush(System.Drawing.Color.DarkKhaki);
            SolidBrush blue = new SolidBrush(System.Drawing.Color.Blue);

            int counter = 0;

            g.DrawRectangle(pen, rect);
            g.FillRectangle(b, rect);

            for (int i = 0; i < code.Length; i++)
            {
                g.DrawString(code[i].ToString(), new Font("Verdena", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
                counter += 20;
            }

            DrawRandomLines(g);
            bitmap.Save("captcha", ImageFormat.Gif);

            //Label l = new label1 = 
            g.DrawImage(bitmap, 10, 10);
            //g.Dispose();
            //bitmap.Dispose();

        }

        public void DrawRandomLines(Graphics g)
        {

            SolidBrush green = new SolidBrush(System.Drawing.Color.Green);
            for (int i = 0; i < 20; i++)
            {
                g.DrawLines(new System.Drawing.Pen(green, 2), GetRandomPoints());
            }
        }

        public System.Drawing.Point[] GetRandomPoints()
        {
            Random rand = new Random();
            System.Drawing.Point[] points = { new System.Drawing.Point(rand.Next(10, 150), rand.Next(10, 150)), new System.Drawing.Point(rand.Next(10, 100), rand.Next(10, 100)) };
            return points;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MemberServiceClient c = new MemberServiceClient();
            CreateImage();
            label8.Content = c.randomText();


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MemberServiceClient c = new MemberServiceClient();
            MemberS x = new MemberS();
            
            

            x.Nama = textBox1.Text;
            x.email = textBox2.Text;
            x.noTelpon = textBox3.Text;
            x.noHP = textBox4.Text;
            x.Alamat = textBox5.Text;
            x.password = passwordBox1.Password;

            x.ID = "M0";

           


            if (!c.register(x, label8.Content.ToString(), textBox6.Text))
                MessageBox.Show("You Are a Bot");

            else
            {
                MessageBox.Show("BERHASIL");
                this.Hide();
            }
        }
    }

}
